<template>
  <div>
    <div class="crud-group">
      <el-select
        v-model="paramsForm.serviceType"
        placeholder="服务类型"
      >
        <el-option
          v-for="item in typeList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-input
        v-model="paramsForm.targetCode"
        placeholder="指标编码"
      />
      <el-input
        v-model="paramsForm.targetName"
        placeholder="指标名称"
      />
      <div>
        <el-button
          icon="el-icon-search"
          type="success"
          @click="getData"
        >搜索</el-button>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="addDialogVisible = true"
        >新增</el-button>
        <el-button
          icon="el-icon-refresh-left"
          type="warning"
          @click="reset"
        >重置</el-button>
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      style="margin-top:10px"
    >
      <el-table-column
        type="index"
        width="55"
      />
      <el-table-column
        label="服务类型"
        prop="serviceType"
      />
      <el-table-column
        label="指标编码"
        prop="targetCode"
      />
      <el-table-column
        label="指标名称"
        prop="targetName"
      />
      <el-table-column
        label="指标来源"
        prop="sourceType"
      />
      <el-table-column
        label="指标sql"
        prop="targetSql"
        show-overflow-tooltip
      />
      <el-table-column
        label="默认值"
        prop="defaultValue"
      />
      <el-table-column
        label="预警值百分比"
        prop="alarmValue"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.alarmValue*100 }}%</span>
        </template>
      </el-table-column>
      <el-table-column
        label="指标个数"
        prop="targetNum"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="del(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :page-sizes="[5, 10, 20, 30, 40]"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :before-close="handleClose"
      :visible.sync="addDialogVisible"
      title="新增"
      width="800px"
    >
      <el-form
        :inline="true"
        label-width="140px"
      >
        <el-form-item label="服务类型:">
          <el-select
            v-model="configForm.serviceType"
            placeholder="请选择"
            style="width:190px"
          >
            <el-option
              v-for="item in typeList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="指标编码:">
          <el-input
            v-model="configForm.targetCode"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标名称:">
          <el-input
            v-model="configForm.targetName"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标来源:">
          <el-input
            v-model="configForm.sourceType"
            placeholder="请输入"
            disabled
          />
        </el-form-item>
        <el-form-item label="指标sql:">
          <el-input
            v-model="configForm.targetSql"
            placeholder="请输入"
            type="textarea"
            style="width:540px"
          />
        </el-form-item>
        <el-form-item label="默认值:">
          <el-input
            v-model="configForm.defaultValue"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="预警值百分比:">
          <el-input
            v-model="configForm.alarmValue"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标个数:">
          <el-input
            v-model="configForm.targetNum"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handleClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="add"
        >确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :before-close="editClose"
      :visible.sync="editDialogVisible"
      title="编辑"
      width="800px"
    >
      <el-form
        :inline="true"
        label-width="140px"
      >
        <el-form-item label="服务类型:">
          <el-select
            v-model="editForm.serviceType"
            placeholder="请选择"
            style="width:190px"
          >
            <el-option
              v-for="item in typeList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="指标编码:">
          <el-input
            v-model="editForm.targetCode"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标名称:">
          <el-input
            v-model="editForm.targetName"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标来源:">
          <el-input
            v-model="editForm.sourceType"
            placeholder="请输入"
            disabled
          />
        </el-form-item>
        <el-form-item label="指标sql:">
          <el-input
            v-model="editForm.targetSql"
            placeholder="请输入"
            type="textarea"
            style="width:540px"
          />
        </el-form-item>
        <el-form-item label="默认值:">
          <el-input
            v-model="editForm.defaultValue"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="预警值百分比:">
          <el-input
            v-model="editForm.alarmValue*100"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标个数:">
          <el-input
            v-model="editForm.targetNum"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="editClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="edit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import parameter from '@/api/phaseTwo/parameter.js'

export default {
  data() {
    return {
      total: 0,
      paramsForm: {
        current: 1,
        size: 10,
        serviceType: '',
        targetCode: '',
        targetName: ''
      },
      data: [],
      isLoading: false,
      addDialogVisible: false,
      editDialogVisible: false,
      permission: {
        add: ['admin', 'add'],
        edit: ['edit'],
        del: ['del']
      },
      configForm: {
        serviceType: '',
        targetCode: '',
        targetName: '',
        sourceType: 'cmapi',
        targetSql: '',
        defaultValue: '',
        alarmValue: '',
        targetNum: ''
      },
      typeList: ['hbase', 'hdfs', 'hive', 'impala', 'yarn', 'zooKeeper', 'other'],
      editFlag: false,
      editForm: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    reset() {
      this.paramsForm = {
        current: 1,
        size: 10,
        serviceType: '',
        targetCode: '',
        targetName: ''
      }
      this.getData()
    },
    getData(resetPage = true) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.isLoading = true
      parameter.getTargetConfig(this.paramsForm).then((res) => {
        this.data = res.content
        this.total = res.totalElements
      }).finally(() => {
        this.isLoading = false
      })
    },
    handleSizeChange(val) {
      this.paramsForm.size = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.paramsForm.current = val
      this.getData(false)
    },
    goCenter() {
      this.$emit('back', '0')
    },
    handleClose() {
      this.configForm = {
        serviceType: '',
        targetCode: '',
        targetName: '',
        sourceType: 'cmapi',
        targetSql: '',
        defaultValue: '',
        alarmValue: '',
        targetNum: ''
      }
      this.addDialogVisible = false
    },
    editClose() {
      this.editDialogVisible = false
      this.editForm = {}
    },
    add() {
      this.configForm.alarmValue = this.configForm.alarmValue / 100
      parameter.addConfig(this.configForm).then((res) => {
        this.handleClose()
        this.getData()
      })
    },
    openEdit(row) {
      this.editForm = row
      this.editDialogVisible = true
    },
    edit() {
      parameter.editConfig(this.editForm).then((res) => {
        this.editClose()
        this.getData()
      })
    },
    del(row) {
      this.$confirm('此操作将永久删除该配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const obj = {id: row.id}
          parameter.delConfig(obj).then((res) => {
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success'
            })
            this.getData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
